/**
 * Created by lenovo on 2017/12/21.
 */
define(['ngApp', 'ngHttp', 'jquery', 'ngConstant', 'ngAlert','bootstrap','datetimepicker','datetimepickerch'], function (ngApp, ngHttp, JQ, ngConstant, ngAlert) {
    ngApp.directive('onFinishRenderFilters', function ($timeout) {
        return {
            restrict: 'A',
            link: function (scope, element, attr) {
                if (scope.$last === true) {
                    $timeout(function () {
                        scope.$emit('ngRepeatFinished');
                    });
                }
            }
        }
    });
    ngApp.controller("leftTab3", function ($scope, $http, $timeout, $rootScope, $state, $uibModal, commonFn, $cookies,$window) {

        $('.form_datetime').datetimepicker({
            //language:  'fr',
            weekStart: 1,
            todayBtn:  1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            forceParse: 0,
            showMeridian: 1
        });
        $('.form_date').datetimepicker({
            language:  'zh-CN',
            weekStart: 1,
            todayBtn:  1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            minView: 2,
            forceParse: 0
        });
        $('.form_time').datetimepicker({
            language:  'fr',
            weekStart: 1,
            todayBtn:  1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 1,
            minView: 0,
            maxView: 1,
            forceParse: 0
        });

        $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
            $rootScope.leftTabHeight = $('#leftTab3').height();
            $('.tabBox').height($rootScope.leftTabHeight);
        });

        //上方  select  接口
        $http({
            url: ngConstant.interfaceHost + 'manager/store/findAll',
            method: 'GET',
            headers: {'Authorization':$cookies.get("token")}
        }).then(function(res){
            $scope.storeAll = res.data.data.content;
            // $scope.storeAllCode =  $scope.storeAll[0].storeName;
        });


        //查询数据的函数封装
        function gettableDataData(){
            if (!$scope.searchClicked) {
                    //查询table中所有数据接口
            $http({
                url: ngConstant.interfaceHost + 'admin/itemUseLog/findSearch',
                method: 'GET',
                params:{
                    "page":$scope.page,
                    "size":ngConstant.size
                },
                headers: {'Authorization':$cookies.get("token")}
            }).then(function(res){
                //页数数据
                $scope.pageMsg  = res.data.data;
                //表格数据
                $scope.tableData = res.data.data.content;
                //全选
                $scope.checked = [];
                $scope.checkedMsg = [];
                $scope.selectAll = function () {
                    if($scope.select_all) {
                        $scope.checked = [];
                        $scope.checkedMsg = [];
                        angular.forEach($scope.tableData, function (i) {
                            i.checked = true;
                            $scope.checked.push(i.id);
                            $scope.checkedMsg.push({"id":i.id});
                        })
                    }else {
                        angular.forEach($scope.tableData, function (i) {
                            i.checked = false;
                            $scope.checked = [];
                            $scope.checkedMsg = [];
                        })
                    }
                };
                $scope.selectOne = function ($event) {
                    angular.forEach($scope.tableData, function (i) {
                        var index = $scope.checked.indexOf(i.id);
                        if(i.checked && index === -1) {
                            $scope.checked.push(i.id);
                            $scope.checkedMsg.push({"id":i.id});
                        } else if (!i.checked && index !== -1){
                            $scope.checked.splice(index, 1);
                            $scope.checkedMsg.splice(index, 1);
                        }
                    });

                    if ($scope.tableData.length === $scope.checked.length) {
                        $scope.select_all = true;
                    } else {
                        $scope.select_all = false;
                    }
                };
            });
            } else {
                    //查询table中所有数据接口
                    $scope.searchMsg.page = $scope.page;
                $scope.searchMsg.size = ngConstant.size;
            $http({
                url: ngConstant.interfaceHost + 'admin/itemUseLog/findSearch',
                method: 'GET',
                params:$scope.searchMsg,
                headers: {'Authorization':$cookies.get("token")}
            }).then(function(res){
                //页数数据
                $scope.pageMsg  = res.data.data;
                //表格数据
                $scope.tableData = res.data.data.content;
                //全选
                $scope.checked = [];
                $scope.checkedMsg = [];
                $scope.selectAll = function () {
                    if($scope.select_all) {
                        $scope.checked = [];
                        $scope.checkedMsg = [];
                        angular.forEach($scope.tableData, function (i) {
                            i.checked = true;
                            $scope.checked.push(i.id);
                            $scope.checkedMsg.push({"id":i.id});
                        })
                    }else {
                        angular.forEach($scope.tableData, function (i) {
                            i.checked = false;
                            $scope.checked = [];
                            $scope.checkedMsg = [];
                        })
                    }
                };
                $scope.selectOne = function ($event) {
                    angular.forEach($scope.tableData, function (i) {
                        var index = $scope.checked.indexOf(i.id);
                        if(i.checked && index === -1) {
                            $scope.checked.push(i.id);
                            $scope.checkedMsg.push({"id":i.id});
                        } else if (!i.checked && index !== -1){
                            $scope.checked.splice(index, 1);
                            $scope.checkedMsg.splice(index, 1);
                        }
                    });

                    if ($scope.tableData.length === $scope.checked.length) {
                        $scope.select_all = true;
                    } else {
                        $scope.select_all = false;
                    }
                };
            });
            }
            
        }
        //搜索
        $scope.searchMsg = {};
        $scope.searchMsg.size = ngConstant.size;
        $scope.searchClicked = false;
        $scope.searcBtnClick = function(){
            $scope.searchClicked = true;
            var store = document.getElementsByName("storeName")[0];
            var storeble= store .options[store.selectedIndex].text;
            if (storeble == '全部') {
                $scope.searchMsg.storeName = '';
            } else {
                $scope.searchMsg.storeName = storeble;
            }

            if ($('.dtpInput1').val() != '') {
                $scope.searchMsg.startTime = $('.dtpInput1').val();
            } else {
                $scope.searchMsg.startTime = '1990/01/01';
            }
            if ($('.dtpInput2').val() != '') {
                $scope.searchMsg.endTime = $('.dtpInput2').val();
            } else {
                $scope.searchMsg.endTime = '2199/01/01';
            }

            console.log($scope.searchMsg);

            $http({
                url: ngConstant.interfaceHost + 'admin/itemUseLog/findSearch',
                method: 'GET',
                params:$scope.searchMsg,
                headers: {'Authorization':$cookies.get("token")}
            }).then(function(res){
                // if(res.data.data.content.length == 0) {
                //     ngAlert.successMsg("暂无数据");
                // } else {
                //     $scope.pageMsg = res.data.data;
                //     $scope.tableData = res.data.data.content;
                // }
                $scope.pageMsg = res.data.data;
                $scope.tableData = res.data.data.content;

            });
        };


        //获取table列表
        $scope.page = 0;
        $scope.page1 = $scope.page + 1;
        $http({
            url: ngConstant.interfaceHost + 'admin/itemUseLog/findSearch',
            method: 'GET',
            params:{
                "page":$scope.page,
                "size":ngConstant.size
            },
            headers: {'Authorization':$cookies.get("token")}
        }).then(function(res){
            //页数数据
            $scope.pageMsg  = res.data.data;
            $scope.tableData = res.data.data.content;
            //全选
            //点击下一页上一页时   $scope.select_all初始化置空   在点击事件中写入
            $scope.checked = [];
            $scope.checkedMsg = [];
            $scope.selectAll  = function () {
                if($scope.select_all) {
                    $scope.checked = [];
                    $scope.checkedMsg = [];
                    angular.forEach($scope.tableData, function (i) {
                        i.checked  = true;
                        $scope.checked.push(i.id);
                        $scope.checkedMsg.push({"id":i.id});
                    })
                }else {
                    angular.forEach($scope.tableData, function (i) {
                        i.checked  = false;
                        $scope.checked = [];
                        $scope.checkedMsg = [];
                    })
                }
            };
            $scope.selectOne = function ($event) {
                angular.forEach($scope.tableData, function (i) {
                    var index = $scope.checked.indexOf(i.id);
                    if(i.checked  && index === -1) {
                        $scope.checked.push(i.id);
                        $scope.checkedMsg.push({"id":i.id});
                    } else if (!i.checked  && index !== -1){
                        $scope.checked.splice(index, 1);
                        $scope.checkedMsg.splice(index, 1);
                    }
                });

                if ($scope.tableData.length === $scope.checked.length) {
                    $scope.select_all  = true;
                } else {
                    $scope.select_all  = false;
                }
            };

            //下一页
            $scope.nextPageBtn  = function(){
                $scope.select_all = false;
                if ($scope.page == $scope.pageMsg .totalPages - 1) {
                    return false;
                } else {
                    $scope.page++;
                    $scope.page1++;
                    gettableDataData();
                }

            };

            //上一页
            $scope.prevPageBtn  = function(){
                $scope.select_all = false;
                if ($scope.page == 0) {
                    return false;
                } else {
                    $scope.page--;
                    $scope.page1--;
                    gettableDataData();
                }
            };

            //转到第几页
            $scope.myKeyup13  = function(e){
                $scope.select_all = false;
                //IE 编码包含在window.event.keyCode中，Firefox或Safari 包含在event.which中
                var keycode = window.event?e.keyCode:e.which;
                if(keycode==13){
                    $scope.page = $scope.page1 - 1;
                    gettableDataData();
                }
            };

            //首页
            $scope.firstPageBtn  = function(){
                $scope.select_all = false;
                if ($scope.page == 0) {
                    return false;
                }
                $scope.page = 0;
                $scope.page1 = 1;
                gettableDataData();
            };

            //尾页
            $scope.lastPageBtn  = function(){
                $scope.select_all = false;
                if ($scope.page == $scope.pageMsg .totalPages - 1){
                    return false;
                }
                $scope.page = $scope.pageMsg .totalPages - 1;
                $scope.page1 = $scope.pageMsg .totalPages;
                gettableDataData();
            };

        });


    })
});